<?php
/**
 * Created by PhpStorm.
 * User: andrei
 * Date: Jun 27, 2012
 * Time: 2:44:11 PM
 * To change this template use File | Settings | File Templates.
 */
 
class Db_Products_Designs extends Common {
	protected static $tableName = TAB_DESIGNS;
	protected static $dbFields = array(
        'id',
		'd_theme_id',
		'd_template_id',
		'd_name',
        'deleted'
	);
	/**
	 * @var Db_Products_Designs
	 */
	public $objectArray;

	public $id;
	public $d_theme_id;
	public $d_template_id;
	public $d_name;
    public $deleted;

    function __construct($value=NULL, $fieldName=NULL) {
        parent::__construct($value, $fieldName);
    }

	public static function getDesigns($sizeId = null, $productId = 0) {
		$sqlJoin = '';
		if(!empty($sizeId)) {
			$sqlJoin = 'INNER JOIN '. TAB_TEMPLATES .' AS t ON t.id = d.d_template_id AND t.t_size_id = ' . $sizeId. ' AND t.deleted = 0 ';
			if($productId) {
				$sqlJoin .= 'INNER JOIN ' . TAB_PRODUCT_THEME_PHOTOS . ' AS pt ON pt.pt_theme_id = d.d_theme_id AND pt.pt_product_id = ' . $productId;
			}
		}

		$sql = "SELECT `d`.*, `d`.`id` as `design_id`, `li`.*, `li`.`id` as `li_id`, `dt`.*, `di`.* FROM ?f `d`
				{$sqlJoin}
				LEFT JOIN ?f `li` ON `li`.`li_owner_id` = `d`.`id` AND `li`.`deleted` = 0 AND `li`.`li_owned_by` = ?
				LEFT JOIN ?f `dt` ON `li`.`id` = `dt`.`dt_layout_item_id` AND `dt`.`deleted` = 0
				LEFT JOIN ?f `di` ON `li`.`id` = `di`.`di_layout_item_id` AND `di`.`deleted` = 0
				WHERE `d`.`deleted` = 0 GROUP BY `d`.`id`;";

		$result = self::$DB->q($sql, self::$tableName, TAB_LAYOUT_ITEMS, 'design', TAB_DESIGN_TEXTS, TAB_DESIGN_IMAGES);
		$dataReturn = array();
		while($data = $result->fetch_object()) {
			$dataReturn[$data->d_theme_id][$data->d_template_id][$data->design_id]['d_name'] = $data->d_name;
			$dataReturn[$data->d_theme_id][$data->d_template_id][$data->design_id]['items'][] = $data;
		}

		return $dataReturn;
	}

	public static function checkDesignExists($id,$templateId,$themeId) {
		return self::$DB->selectCell("SELECT `id` FROM ?f WHERE `d_template_id` = ?d AND `d_theme_id` = ?d AND `id` != ?d AND `deleted` = 0 ;", self::$tableName, $templateId, $themeId, $id);
	}

	public static function getDesignsByTemplateId($templateId) {
		if(!$templateId) {
			return false;
		}
		return self::$DB->select("SELECT `id` FROM ?f WHERE `d_template_id` = ?d AND `deleted` = 0 ;", self::$tableName, $templateId);
	}

    public static function getAllDesigns($limit, $sqlSearch = '', $orderBy = '') {

        if (empty($sqlSearch)) {
            $sqlSearch = '1=1';
			$joinSearch = '';
        } else {
			$joinSearch = " LEFT JOIN " . TAB_TRANSLATIONS . " tr ON tr.t_module = d.d_module AND tr.t_key = d.d_name AND tr.t_lang_id = " . conf('COUNTRY_LANG_ID') ;
			$joinSearch .= " LEFT JOIN " . TAB_TRANSLATIONS . " tr2 ON tr2.t_module = t.t_module AND tr2.t_key = t.t_name AND tr2.t_lang_id = " . conf('COUNTRY_LANG_ID') ;

			$sqlSearch .= " OR (`tr`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
			$sqlSearch .= " OR (`tr2`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
			}
		if(empty($orderBy)) {
			$orderBy = 'ORDER BY `d`.`id`';
		}

        $sql = "SELECT `te`.`te_name`, `t`.`t_name`, `d`.`d_name`, `d`.`id` AS `d_id` FROM ?f AS `d`
                INNER JOIN ?f AS `t` ON `t`.`id` = `d`.`d_template_id` AND `t`.`deleted` = 0 AND `d`.`deleted` = 0
                INNER JOIN ?f AS `te` ON `te`.`id` = `d`.`d_theme_id` AND `te`.`deleted` = 0
                 {$joinSearch}
                WHERE t.deleted = 0 AND {$sqlSearch} GROUP BY `d`.`id` {$orderBy} {$limit};";
        return self::$DB->select($sql, self::$tableName, TAB_TEMPLATES, TAB_THEMES );
    }

	public static function getTotalDesigns($sqlCondition = '1=1') {
		if (empty($sqlCondition)) {
			$sqlCondition = '1=1';
			$joinSearch = '';
		} else {
			$joinSearch = " LEFT JOIN " . TAB_TRANSLATIONS . " tr ON tr.t_module = d.d_module AND tr.t_key = d.d_name AND tr.t_lang_id = " . conf('COUNTRY_LANG_ID') ;
			$joinSearch .= " LEFT JOIN " . TAB_TRANSLATIONS . " tr2 ON tr2.t_module = t.t_module AND tr2.t_key = t.t_name AND tr2.t_lang_id = " . conf('COUNTRY_LANG_ID') ;

			$sqlCondition .= " OR (`tr`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
			$sqlCondition .= " OR (`tr2`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
		}
		$sql = "SELECT COUNT(*) FROM ?f AS `d`
                INNER JOIN ?f AS `t` ON `t`.`id` = `d`.`d_template_id` AND `t`.`deleted` = 0 AND `d`.`deleted` = 0
                INNER JOIN ?f AS `te` ON `te`.`id` = `d`.`d_theme_id` AND `te`.`deleted` = 0
                {$joinSearch}
                WHERE {$sqlCondition};";

		return self::$DB->selectCell($sql, self::$tableName, TAB_TEMPLATES, TAB_THEMES );
	}

}